import {Component, Input, OnInit, ViewChild} from '@angular/core';
import {CommonService} from '../common.service';
import {$t} from 'codelyzer/angular/styles/chars';

@Component({
    selector: 'app-comment',
    templateUrl: './comment.component.html',
    styleUrls: ['./comment.component.css']
})
export class CommentComponent implements OnInit {
    @ViewChild('paginator') paginator;
    @Input() id;
    comments = {
        current_page: '',
        data: [],
        last_page: '',
        per_page: '',
        total: '',
    };
    commentInfo = {
        parent_id: 0,
        to_uid: 0,
        content: '',
        verify: '',
        key: '',
        vid: '',
    };
    verify;
    index;
    code;
    key;
    loading = true;
    params;

    constructor(private Service: CommonService) {
    }

    ngOnInit() {
        this.getList();
        this.getCode();
    }


    getList(url = document.location.toString()) {
        const $this = this;
        const params = this.Service.getUrlParams(url);
        params['id'] = this.id;
        params['row'] = 10;
        this.params = params;

        this.Service.post('comment/getlist', params).subscribe(res => {
            $this.Service.resultHandler(res, function (msg, data) {
                $this.comments = data;
                console.log(data)
                if (!data.total) {
                    $this.loading = false;
                }
            });
        });
    }

    /**
     * 获取验证码
     */
    getCode() {
        const $this = this;
        this.Service.post('comment/verify', {}).subscribe(res => {
            this.Service.resultHandler(res, function (msg, data) {
                $this.verify = data.img;
                $this.code = data.code;
                $this.key = data.key;
                $this.commentInfo.key = data.key;
            });
        });
    }

    /**
     * 发布评论
     */
    comment() {
        const $this = this;
        const user = this.Service.getUserInfo();
        let verify = this.commentInfo.verify;
        let code = this.code;
        if (!user) {
            return layer.msg('您还未登录，请登录后再来~');
        }
        let params = {
            'uid': user.id,
            'nickname': user.nickname
        };

        if (code.toLowerCase() != verify.toLowerCase()) {
            $this.getCode();
            return layer.msg('验证码错误~');
        }
        if (this.commentInfo.content.length < 5 || this.commentInfo.content.length > 100) {
            $this.getCode();
            return layer.msg('评论内容长度不符合规则~');
        }
        let regexp = /((((13[0-9])|(15[^4])|(18[0,1,2,3,5-9])|(17[0-8])|(147))\d{8})|((\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}))|[0-9]{5,10}?/g;
        if (this.commentInfo.content.match(regexp)) {
            $this.getCode();
            return layer.msg('评论内容不能包含联系方式，和言语辱骂等不良言论~');
        }
        params = $.extend(params, this.commentInfo);
        params['vid'] = this.id;

        let index = layer.load();
        this.Service.post('comment/create', params).subscribe(res => {
            this.Service.resultHandler(res, function (msg, data) {
                $this.getCode();
                layer.close(index);
                if (res['status']) {
                    const comments = $this.comments;
                    if($this.commentInfo.parent_id){
                        comments.data[$this.index]['child']['data'].unshift(data);
                    }else{
                        data['child'] = {
                            'data':[],
                            'total':0
                        }
                        comments['data'].unshift(data);
                        comments.total += 1;
                    }
                    $this.comments = comments;
                    layer.msg('评论成功');
                } else {
                    layer.msg(msg);
                }
            });
        });
    }

    /**
     * 设置回复父级参数
     * @param parent_id
     * @param to_uid
     * @param to_nickname
     */
    setParent(parent_id, to_uid, to_nickname, key) {
        this.index = key;
        this.commentInfo.parent_id = parent_id;
        this.commentInfo.to_uid = to_uid;
        this.commentInfo.content = '@' + to_nickname + ' ';
        let height = window.document.body.offsetHeight;
        window.scroll(0,height)
    }

    /**
     * 点赞
     * @param id
     * @param key
     */
    liked(id,key) {
        const $this = this;
        const user = this.Service.getUserInfo();
        if (!user) {
            return layer.msg('您还未登录，请登录后再来~');
        }
        let params = {
            'id': id,
            'uid': user.id,
        };

        let index = layer.load();
        this.Service.post('comment/liked', params).subscribe(res => {
            this.Service.resultHandler(res, function (msg, data) {
                layer.close(index);
                if (res['status']) {
                    $this.comments.data[key]['goods'] = parseInt($this.comments.data[key]['goods'])+1
                    layer.msg('点赞成功');
                } else {
                    layer.msg(msg);
                }
            });
        });
    }

    /**
     * 显示更多评论
     * @param pid
     */
    showMore(pid) {
        $('.comment-' + pid + ' .comment-hide').show();
        $('.comment-' + pid + ' .comment-footer1').hide();
    }

    /**
     * 分页跳转
     * @param e
     */
    goLink(e) {
        this.getList(e.url);
    }
}
